﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using BUS;
using Utility;

public partial class Administrator_sanpham_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username_dn"] != null)
            {
                LoadDropDownList();
                load_data();
            }
            else
            {
                Response.Redirect("~/A_LetsDeal-DoanhNghiep/Default.aspx");
            }
        }
    }
    private void LoadDropDownList()
    {
        Tbl_LoaiSP_Collection loaiCollection = Tbl_LoaiSP_BUS.GetAll();
        ddl_loai.DataTextField = Tbl_LoaiSP_Columns.TenLoai.ToString();
        ddl_loai.DataValueField = Tbl_LoaiSP_Columns.STT.ToString();
        ddl_loai.DataSource = loaiCollection;
        ddl_loai.DataBind();
    }
    private void load_data()
    {
        Literal3.Text = Tbl_DoanhNghiep_BUS.GetByName(Session["username_dn"].ToString()).TenDN;
    }

    protected void btn_Add_Click(object sender, EventArgs e)
    {
        Tbl_SanPham_Entities sanpham = new Tbl_SanPham_Entities();

        sanpham.MaSP = txt_MaSP.Text;
        sanpham.TenSP = ftb_tensp.Text;
        sanpham.GiaGoc = float.Parse(txt_giagoc.Text);
        sanpham.GiaBan = float.Parse(txt_giaban.Text);
        float giagoc = float.Parse(txt_giagoc.Text);
        float giaban = float.Parse(txt_giaban.Text);
        float tyle = float.Parse(((100 - giaban * 100 / giagoc) / 100).ToString());
        sanpham.TyLeGiamGia = round_float(tyle);
        sanpham.NgayDang = DateTime.Now.ToString();
        sanpham.NgayBan = "";
        sanpham.NgayKetThucDuKien = "";
        sanpham.NgayKetThuc = "";
        sanpham.TinhTrang = "Chưa Xét";
        sanpham.ThongTinCoBan = ftb_tt.Text;
        sanpham.ThongTinDayDu = ftb_ttct.Text;
        sanpham.ThoiGianSuDung = "";
        sanpham.DKSD = ftb_dksd.Text;
        sanpham.SLBan = int.Parse(txt_slban.Text);
        sanpham.DoanhNghiep = Tbl_DoanhNghiep_BUS.GetByName(Session["username_dn"].ToString()).STT;
        sanpham.LoaiSP = int.Parse(ddl_loai.SelectedValue);
        
        int stt_sp = Tbl_SanPham_BUS.Insert(sanpham);

        Tbl_ImageSP_Entities image_sp = new Tbl_ImageSP_Entities();
        image_sp.ImagePath = "noimages.jpg";
        image_sp.SanPham = stt_sp;
        Tbl_ImageSP_BUS.Insert(image_sp);

        Literal1.Visible = true;
        Literal1.Text = "Thêm thông tin sản phẩm thành công";
        HyperLink1.Visible = true;
    }
    protected float round_float(float n)
    {
        int t, u;
        t = Convert.ToInt32(n * 1000);
        u = Convert.ToInt32(t % 10);

        if (u >= 5)
        {
            u = t / 10 + 1;
        }
        else
            u = t / 10;
        n = (float)u / 100;

        return n;


    }
}
